package com.gisterrier.client.components;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.TreeMap;

import com.common.shared.ClSelectionItem;
import com.gisterrier.client.GisTerrier;
import com.gisterrier.shared.ClSelection;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class PMapStreetTool extends VLayout {
	private SelectItem siRegion;
	private SelectItem siSubRegion;
	private SelectItem siCity;
	private SelectItem siStreet;
	private TextItem tiMapStreet;

	private boolean settingMyself = false;

	public PMapStreetTool() {
		setHeight100();
		setWidth100();
		this.setMembersMargin(20);
		DynamicForm form = new DynamicForm();

		form.setWidth100();
		// form.setBorder("1px solid red");
		form.setTitleAlign(Alignment.LEFT);

		siRegion = new SelectItem();
		siRegion.setTitle("Region");
		siRegion.setWidth("100%");

		siSubRegion = new SelectItem();
		siSubRegion.setTitle("Sub Region");
		siSubRegion.setWrapTitle(false);
		siSubRegion.setWidth("100%");

		siCity = new SelectItem();
		siCity.setTitle("City");
		siCity.setWidth(200);
		siCity.setWidth("100%");

		siStreet = new SelectItem();
		siStreet.setTitle("Bill. Street");
		siStreet.setWidth("100%");
		form.setLayoutAlign(Alignment.LEFT);

		tiMapStreet = new TextItem();
		tiMapStreet.setTitle("Map Street");
		tiMapStreet.setWidth("100%");
		tiMapStreet.setAttribute("readOnly", "true");
		form.setLayoutAlign(Alignment.LEFT);

		form.setFields(siRegion, siSubRegion, siCity, siStreet, tiMapStreet);

		this.addMember(form);
		final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("-1", "--");
		siSubRegion.setValueMap(map);
		siSubRegion.setValue("-1");
		siCity.setValueMap(map);
		siCity.setValue("-1");
		siStreet.setValueMap(map);
		siStreet.setValue("-1");

		GisTerrier.gissTerrierService
				.getAllTopTypes(new AsyncCallback<TreeMap<Integer, ArrayList<ClSelectionItem>>>() {

					@Override
					public void onSuccess(
							TreeMap<Integer, ArrayList<ClSelectionItem>> result) {
						setSelectItems(siRegion,
								result.get(ClSelection.T_REGION));
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
		siRegion.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				if (settingMyself)
					return;
				siSubRegion.setValueMap(map);
				siSubRegion.setValue("-1");
				siCity.setValueMap(map);
				siCity.setValue("-1");
				siStreet.setValueMap(map);
				siStreet.setValue("-1");
				long val = Long.parseLong(event.getValue().toString());
				if (val < 0)
					return;
				GisTerrier.gissTerrierService.getSubRegions(val,
						new AsyncCallback<ArrayList<ClSelectionItem>>() {

							@Override
							public void onSuccess(
									ArrayList<ClSelectionItem> result) {
								setSelectItems(siSubRegion, result);

							}

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
						});

			}
		});
		siSubRegion.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				if (settingMyself)
					return;
				siCity.setValueMap(map);
				siCity.setValue("-1");
				siStreet.setValueMap(map);
				siStreet.setValue("-1");
				long val = Long.parseLong(event.getValue().toString());
				if (val < 0)
					return;
				GisTerrier.gissTerrierService.getCities(val,
						new AsyncCallback<ArrayList<ClSelectionItem>>() {

							@Override
							public void onSuccess(
									ArrayList<ClSelectionItem> result) {
								setSelectItems(siCity, result);

							}

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
						});

			}
		});
		siCity.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				if (settingMyself)
					return;
				siStreet.setValueMap(map);
				siStreet.setValue("-1");
				long val = Long.parseLong(event.getValue().toString());
				if (val < 0)
					return;
				GisTerrier.gissTerrierService.getStreets(val,
						new AsyncCallback<ArrayList<ClSelectionItem>>() {

							@Override
							public void onSuccess(
									ArrayList<ClSelectionItem> result) {
								setSelectItems(siStreet, result);

							}

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
						});

			}
		});

		HLayout dynamicForm = new HLayout();
		dynamicForm.setMembersMargin(10);

		Button bApplyToMap = new Button("Go to Object");
		dynamicForm.setAlign(Alignment.CENTER);
		dynamicForm.addMember(bApplyToMap);
		this.addMember(dynamicForm);

	}

	private void setSelectItems(SelectItem si, ArrayList<ClSelectionItem> items) {
		if (items == null) {
			items = new ArrayList<ClSelectionItem>();
		}
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("-1", "--");
		for (ClSelectionItem item : items) {
			map.put(item.getId() + "", item.getValue());
		}
		si.setValueMap(map);
		si.setValue("-1");
	}
}
